//
//  ABUDCustomNativeAdapter.m
//  ABUDemo
//
//  Created by bytedance on 2021/10/21.
//  Copyright © 2021 bytedance. All rights reserved.
//

#import "JiGuangAdsNativeAdapter.h"
#import <JgAdsSDK/JgAdsSDK.h>
@interface JiGuangAdsNativeAdapter ()<JgAdsNativeExpressAdDelegate>

@property (nonatomic, strong) JgAdsNativeExpressAd *nativeAd;

@property (nonatomic, strong) UIView<JgAdsExpressViewRegisterProtocol> *nativeView;

@property (nonatomic, assign) BOOL isBidType;
@end

@implementation JiGuangAdsNativeAdapter

- (BUMMediatedAdStatus)mediatedAdStatus {
    return BUMMediatedAdStatusNormal;
}

- (void)loadNativeAdWithSlotID:(nonnull NSString *)slotID andSize:(CGSize)size imageSize:(CGSize)imageSize parameter:(nonnull NSDictionary *)parameter {
    if([parameter[BUMAdLoadingParamBiddingType] integerValue] == 1){
        self.isBidType = YES;
    }else{
        self.isBidType = NO;
    }
    // 获取广告加载数量
    int count = [parameter[BUMAdLoadingParamNALoadAdCount] intValue];
    if (self.isBidType) {
        count = 1;
    }
    _nativeAd = [[JgAdsNativeExpressAd alloc]initWithAdSize:size];
    _nativeAd.delegate = self;
    _nativeAd.controller = self.bridge.viewControllerForPresentingModalView;
    _nativeAd.posId = slotID;
    [_nativeAd loadAdWithCount:count];
}

- (void)registerContainerView:(nonnull __kindof UIView *)containerView andClickableViews:(nonnull NSArray<__kindof UIView *> *)views forNativeAd:(nonnull id)nativeAd {
//    if ([nativeAd isKindOfClass:[BUMDCustomNativeData class]]) {
//        BUMDCustomNativeData *ad = (BUMDCustomNativeData *)nativeAd;
//        [ad registerClickableViews:views];
//    }
}

- (void)renderForExpressAdView:(nonnull UIView *)expressAdView {
    if (self.isBidType) {
        [self.nativeAd sendWinNotificationWithAdView:self.nativeView price:self.nativeView.bidPrice];
    }
    if ([[NSThread currentThread] isMainThread]) {
        [self.nativeView jgads_registViews:@[self.nativeView]];
    }else {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.nativeView jgads_registViews:@[self.nativeView]];
        });
    }
}

- (void)setRootViewController:(nonnull UIViewController *)viewController forExpressAdView:(nonnull UIView *)expressAdView {
//    if ([expressAdView isKindOfClass:[BUMDCustomExpressNativeView class]]) {
//        BUMDCustomExpressNativeView *view = (BUMDCustomExpressNativeView *)expressAdView;
//        view.viewController = viewController;
//    }
}

- (void)setRootViewController:(nonnull UIViewController *)viewController forNativeAd:(nonnull id)nativeAd {
//    if ([nativeAd isKindOfClass:[BUMDCustomNativeData class]]) {
//        BUMDCustomNativeData *ad = (BUMDCustomNativeData *)nativeAd;
//        ad.viewController = viewController;
//    }
}

-(void)didReceiveBidResult:(BUMMediaBidResult *)result{
    if (!self.nativeAd) return;
    if (result.win) {
        NSLog(@"信息流==========竞价成功!竞赢价格：%ld",(long)result.winnerPrice);
    } else {
        NSLog(@"信息流==========竞价失败");
        JgAdsBiddingLossReason reason ;
        if ([result.lossDescription isEqualToString:@"timeout"]) {
            reason = JgAdsBiddingLossReasonOther;
        }else if ([result.lossDescription isEqualToString:@"low price"]){
            reason = JgAdsBiddingLossReasonLowPrice;
        }else {
            reason = JgAdsBiddingLossReasonOther;
        }
        [self.nativeAd sendWinFailNotificationReason:reason winnerPirce:result.winnerPrice AdView:self.nativeView];
       
    }
}
#pragma mark - JgAdsNativeExpressAdDelegate

/**
 信息流广告加载成功
 */
- (void)jgadsExpressAdSucceedToLoad:(JgAdsNativeExpressAd *)expressAd views:(NSArray<__kindof UIView<JgAdsExpressViewRegisterProtocol> *> *)views {
    NSMutableArray *exts = [NSMutableArray array];
    NSMutableDictionary *parames = [NSMutableDictionary dictionary];
    if (views.count > 0) {
        self.nativeView = views.firstObject;
        if (self.isBidType && self.nativeView.bidPrice > 0) {
#if DEBUG
        NSLog(@"极光ads信息流===========================竞价的价格：%ld",(long)self.nativeView.bidPrice);
#endif
            [parames setValue:[NSString stringWithFormat:@"%ld",self.nativeView.bidPrice] forKey:BUMMediaAdLoadingExtECPM];
            [exts addObject:parames];
        }
    }
    [self.bridge nativeAd:self didLoadWithExpressViews:[views copy] exts:exts.copy];
}

/**
 信息流广告加载失败
 */
- (void)jgadsExpressAdFailToLoad:(JgAdsNativeExpressAd *)expressAd error:(NSError *)error {
    [self.bridge nativeAd:self didLoadFailWithError:error];
}

/**
 信息流广告渲染成功
 */
- (void)jgadsExpressAdRenderSucceed:(JgAdsNativeExpressAd *)expressAd adView:(UIView<JgAdsExpressViewRegisterProtocol> *)expressAdView {
    [self.bridge nativeAd:self renderSuccessWithExpressView:expressAdView];
}

/**
 信息流广告渲染失败
 */
- (void)jgadsExpressAdRenderFail:(JgAdsNativeExpressView *)expressAd error:(NSError *)error {
    [self.bridge nativeAd:self renderFailWithExpressView:expressAd andError:error];
}

/**
 信息流广告关闭
 */
- (void)jgadsExpressAdClosed:(JgAdsNativeExpressAd *)expressAd adView:(UIView<JgAdsExpressViewRegisterProtocol> *)expressAdView {
    [self.bridge nativeAd:self didCloseWithExpressView:expressAdView closeReasons:nil];
}

/**
 信息流广告点击
 */
- (void)jgadsExpressAdClick:(JgAdsNativeExpressAd *)expressAd adView:(UIView<JgAdsExpressViewRegisterProtocol> *)expressAdView {
    [self.bridge nativeAd:self videoDidClick:expressAdView];
}

/**
 信息流广告展示
 */
- (void)jgadsExpressAdDidExpourse:(JgAdsNativeExpressAd *)expressAd adView:(UIView<JgAdsExpressViewRegisterProtocol> *)expressAdView {
    [self.bridge nativeAd:self didVisibleWithMediatedNativeAd:expressAdView];
}

/**
 *以下为视频信息流相关回调
 */

/**
 信息流视频广告开始播放
 */
- (void)jgadsExpressAdVideoPlay:(JgAdsNativeExpressAd *)expressAd adView:(UIView<JgAdsExpressViewRegisterProtocol> *)expressAdView {
    
}

/**
 信息流视频广告视频播放失败
 */
- (void)jgadsExpressAdVideoPlayFail:(JgAdsNativeExpressAd *)expressAd adView:(UIView<JgAdsExpressViewRegisterProtocol> *)expressAdView error:(NSError *)error {
    
}

/**
 信息流视频广告视频暂停
 */
- (void)jgadsExpressAdVideoPause:(JgAdsNativeExpressAd *)expressAd adView:(UIView<JgAdsExpressViewRegisterProtocol> *)expressAdView {
    
}

/**
 信息流视频广告视频播放完成
 */
- (void)jgadsExpressAdVideoFinish:(JgAdsNativeExpressAd *)expressAd adView:(UIView<JgAdsExpressViewRegisterProtocol> *)expressAdView {
    
}

/**
 信息流广告关闭落地页
 */
- (void)jgadsExpressAdDidCloseLandingPage:(JgAdsNativeExpressAd *)expressAd adView:(UIView<JgAdsExpressViewRegisterProtocol> *)expressAdView{
    [self.bridge nativeAd:self didDismissFullScreenModalWithMediatedNativeAd:expressAdView];
}

- (void)jgadsExpressAdDidPresent:(nonnull JgAdsNativeExpressAd *)expressAd adView:(nonnull UIView<JgAdsExpressViewRegisterProtocol> *)expressAdView {
    
}


@end
